window.onload = function () {
    let userInput = document.querySelector(`.userInput`);

    let startBtn = document.querySelector(`.start`);
    let output = document.querySelector(`.output`);
    let NEle = document.querySelector(`.N`);
    let N = 100;

    NEle.onblur = function () {
        N = +NEle.value;
    };

    startBtn.addEventListener("click", () => {
        // 读取userInput里面的内容
        let str = userInput.value;
        // 1/i
        let f = eval("n => " + str);
        let res = calc(N, f);
        output.innerHTML = res.toString();
    });

    /**
     * 计算一个数列的前N项和
     * @param n
     * @param func 数列的通向公式 的函数的对象
     */
    function calc(n, func) {
        let res = 0;
        for (let i = 1; i <= n; i++) {
            res += func(i);
        }
        return res;
    }
}
